package hhm.wechat.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import hhm.wechat.constants.Wechat;
import hhm.wechat.mybatis.pojo.WxMeta;
import hhm.wechat.utils.WeixinUtil;

@Controller
@RequestMapping("/media")
@ResponseBody
public class MediaController {

	@RequestMapping("/get/{media_id}")
	public Object get(@PathVariable String media_id) {

		return WeixinUtil.getMedia(media_id,
				"dNazxTe6Hld2yoOWK-vChUdh_MYvFNQkR6qlmNiDd6-2o2YbaV37murv5fGVsuetfZ0mOkjZexRxmLKIb56XCaHaVi_kDbUL66E2ck3XJknrWhHdI7G89_bq_cARx4ECPGRgAGAXOZ");

	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public Object upload(@RequestParam MultipartFile[] files, HttpSession session) {

		WxMeta meta = new WxMeta();
		meta = (WxMeta) session.getAttribute(Wechat.ACCESS_TOKEN);

		for (int i = 0; i < files.length; i++) {
			File file = null;
			try {
				file = multipartToFile(files[i]);

				WeixinUtil.upload(file, meta.getMetaValue(), "image");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		UploadMsg um = new UploadMsg();
		return new UploadMsg();

	}

	private File multipartToFile(MultipartFile multfile) throws IOException {
		CommonsMultipartFile cf = (CommonsMultipartFile) multfile;
		// 这个myfile是MultipartFile的
		DiskFileItem fi = (DiskFileItem) cf.getFileItem();
		File file = fi.getStoreLocation();
		// 手动创建临时文件
		if (file.length() < 10240000) {
			File tmpFile = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator")
					+ multfile.getOriginalFilename());
			multfile.transferTo(tmpFile);

			return tmpFile;
		}
		return file;
	}
}

class UploadMsg {
	private String success;
	private String msg;
	private String path;
	private String url;
	private String ori_name;
	private String md5_name;

	public String getSuccess() {
		return success;
	}

	public void setSuccess(String success) {
		this.success = success;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getOri_name() {
		return ori_name;
	}

	public void setOri_name(String ori_name) {
		this.ori_name = ori_name;
	}

	public String getMd5_name() {
		return md5_name;
	}

	public void setMd5_name(String md5_name) {
		this.md5_name = md5_name;
	}

}
